package com.fivetiger.service.impl;

import com.fivetiger.dao.AdminDao;
import com.fivetiger.entity.Admin;
import com.fivetiger.service.inte.AdminService;
import com.fivetiger.util.RUtil;
import com.fivetiger.util.StringUtil;
import com.fivetiger.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao dao;

    @Override
    public boolean login(String admin, String password, HttpSession session) {
        if(StringUtil.isnotNull(admin ,password)){
            Admin admin1 = dao.selectByName(admin);
            if(admin1 != null){
                if (admin1.getPassword().equals(DigestUtils.md5DigestAsHex(admin.getBytes()))){
                    session.setAttribute("admin",admin1);
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public R save(Admin admin) {
        if(admin !=null && admin.getAdmin()!=null){
            admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
            if(dao.insert(admin) > 0){
                return RUtil.ok();
            }
        }
        return RUtil.fail();
    }
}
